package multi.data;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Browse extends Activity implements OnItemClickListener {
	
	private TextView ket;
	private ListView list;
	private Button add;
	
	private List<String> item = null;
	private List<String> path = null;
	private final String root = "/sdcard";
	
	ListViewAssist adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse);
		ket = (TextView) findViewById(R.id.textViewBrowse);
		list = (ListView) findViewById(R.id.listViewBrowse);
		add = (Button) findViewById(R.id.buttonBrowse);
		
		add.setText("Add");
		add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent editor = new Intent(getApplicationContext(), Editor.class);
				editor.putExtra("images", "images");
				startActivity(editor);
			}
		});
		getDir("/sdcard/DCIM/Camera");
	}

	private void getDir(String dirPath) {
		ket.setText("Location: " + dirPath);

		item = new ArrayList<String>();
		path = new ArrayList<String>();

		File f = new File(dirPath);
		File[] files = f.listFiles();

		if (!dirPath.equals(root)) {
			item.add(root);
			path.add(root);

			item.add("../");
			path.add(f.getParent());
		}

		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			path.add(file.getPath());
			if (file.isDirectory())
				item.add(file.getName() + "/");
			else
				item.add(file.getPath());
		}

		adapter = new ListViewAssist(getApplicationContext(), R.layout.listview, item);
		list.setAdapter(adapter);
		list.setOnItemClickListener(this);
	}

	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		File file = new File(path.get(position));

		if (file.isDirectory()) {
			if (file.canRead())
				getDir(path.get(position));
			else {
			}
		} else {
			// aktifin checkbox
		}
	}
}